package com.pls.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.pls.constants.dbConstants;
import com.pls.domain.Employee;
import com.pls.domain.EmployeeWorkstation;
import com.pls.domain.Seat;
import com.pls.utilities.ConnectionManager;

public class SeatDAOImpl implements SeatDAO {
	
	Logger logger = Logger.getLogger(EmployeeDAOImpl.class);

	private CallableStatement proc = null;
	private ResultSet rs = null;
	private ConnectionManager conManager;
	private Connection connection = null;
	
	private List<EmployeeWorkstation> employeeWorkstation;
	
	@Override
	public boolean insertNewEmployeeSeat(EmployeeWorkstation employeeWorkstation) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateEmployeeSeat(Employee employee, String newSeatCode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteEmployeeSeat(Employee employee) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<EmployeeWorkstation> getSeatPlanByFloor(String location,
			String floor) throws SQLException, Exception {
		logger.debug("Getting floor and location for view seat map.");
		
		employeeWorkstation = new ArrayList<EmployeeWorkstation>();
		try {
			conManager = ConnectionManager.getInstance();
			connection = conManager.getConnection();
			proc = connection.prepareCall(dbConstants.PROC_GET_SEATMAP_BY_FLOOR);

			floor = location + floor;
			proc.setString(1, floor);
			rs = proc.executeQuery();

			EmployeeWorkstation empWorkStation;

			while (rs.next()) {
				empWorkStation = new EmployeeWorkstation();

				Employee employeeDetails = new Employee();

				employeeDetails.setFirstName(rs.getString("first_name"));
				employeeDetails.setLastName(rs.getString("last_name"));

				Seat seatDetails = new Seat();
				seatDetails.setSeatCode(rs.getString("seat_code"));
				seatDetails.setLocalNumber(rs.getInt("local_no"));

				empWorkStation.setSeatDetails(seatDetails);
				empWorkStation.setEmployee(employeeDetails);
				employeeWorkstation.add(empWorkStation);

			}
			rs.close();
			logger.debug("Method getSeatPlanByFloor successfully executed.");
		} catch (SQLException e) {
			// Do error logs here
			throw e;
		} catch (Exception e) {
			// Do error logs here
			throw e;
		}
		logger.debug("end of method getSeatPlanByFloor");
		return employeeWorkstation;
		
	}

	@Override
	public List<EmployeeWorkstation> getSeatPlanByQuadrant(String location,
			String quadrant) throws SQLException, Exception {
		logger.debug("Getting location and quadrant for view seat by quadrant.");
		// gets seat map and details of employees for a particular quadrant for
		// a corresponding location
employeeWorkstation = new ArrayList<EmployeeWorkstation>();

		try {
			conManager = ConnectionManager.getInstance();
			connection = conManager.getConnection();
			proc = connection.prepareCall(dbConstants.PROC_Get_SEATMAP_BY_QUADRANT);
			
			quadrant = location + quadrant;
			proc.setString(1, quadrant);
			rs = proc.executeQuery();

			EmployeeWorkstation empWorkStation;

			while (rs.next()) {
				empWorkStation = new EmployeeWorkstation();

				Employee employeeDetails = new Employee();

				employeeDetails.setFirstName(rs.getString("first_name"));
				employeeDetails.setLastName(rs.getString("last_name"));

				Seat seatDetails = new Seat();
				seatDetails.setSeatCode(rs.getString("seat_code"));
				seatDetails.setLocalNumber(rs.getInt("local_no"));

				empWorkStation.setSeatDetails(seatDetails);
				empWorkStation.setEmployee(employeeDetails);
				employeeWorkstation.add(empWorkStation);

			}

		} finally {
			try {
				proc.close();
			} catch (SQLException e) {
			}
			rs.close();
			logger.debug("Method getSeatPlanByFloor successfully executed.");
		}

		return employeeWorkstation;
	}
	
	public List<EmployeeWorkstation> getSeatCodesByFloor (String location,
			String floor) throws SQLException, Exception {

	// gets all seatcodes
		logger.debug("Entered getSeatCods method");
		employeeWorkstation = new ArrayList<EmployeeWorkstation>();
	try {
		conManager = ConnectionManager.getInstance();
		connection = conManager.getConnection();
		proc = connection.prepareCall(dbConstants.PROC_GET_ALL_SEAT_CODES);

		floor = location + floor;
		proc.setString(1, floor);
		rs = proc.executeQuery();


		EmployeeWorkstation empWorkStation;

		while (rs.next()) {
			empWorkStation = new EmployeeWorkstation();

			Employee employeeDetails = new Employee();

			Seat seatDetails = new Seat();
			seatDetails.setSeatCode(rs.getString("seat_code"));
			seatDetails.setSeatType(rs.getString("seat_type_code"));

			empWorkStation.setSeatDetails(seatDetails);
			empWorkStation.setEmployee(employeeDetails);
			employeeWorkstation.add(empWorkStation);

		}

	} finally {
		try {
			proc.close();
		} catch (SQLException e) {
		}
		rs.close();
		logger.debug("Method getSeatPlanByFloor successfully executed.");
	}

	return employeeWorkstation;
	}


}
